package com.ship.dispatch.service.fob.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ship.common.core.exception.BusException;
import com.ship.common.core.web.domain.BaseResultCode;
import com.ship.dispatch.bean.ConShipping;
import com.ship.dispatch.bean.SpFobSublease;
import com.ship.dispatch.bean.TzFuelDelivery;
import com.ship.dispatch.bean.fob.*;
import com.ship.dispatch.bean.fob.SpFobSubleaseRedeliveryUpdate;
import com.ship.dispatch.bpm.utils.JsonUtils;
import com.ship.dispatch.bpm.vo.ManageEntityVo;
import com.ship.dispatch.mapper.fob.SpFobSubleaseRedeliveryUpdateMapper;
import com.ship.dispatch.mapper.fob.SpFobSubleaseRedeliveryUpdateMapper;
import com.ship.dispatch.service.ConShippingService;
import com.ship.dispatch.service.SpDispatchSchemeExecuteService;
import com.ship.dispatch.service.SpFobSubleaseService;
import com.ship.dispatch.service.TzFuelDeliveryService;
import com.ship.dispatch.service.fob.SpFobFileRecordService;
import com.ship.dispatch.service.fob.SpFobSubleaseRedeliveryUpdateService;
import com.ship.dispatch.service.fob.SpFobTemplateService;
import com.ship.dispatch.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SpFobSubleaseRedeliveryUpdateServiceImpl extends FobBaseServiceImpl<SpFobSubleaseRedeliveryUpdateMapper, SpFobSubleaseRedeliveryUpdate> implements SpFobSubleaseRedeliveryUpdateService {

    @Autowired(required = false)
    SpFobSubleaseRedeliveryUpdateMapper mapperapper;

    @Autowired
    SpFobFileRecordService spFobFileRecordService;

    @Autowired
    SpDispatchSchemeExecuteService schemeDetailService;

    @Autowired
    SpFobTemplateService spFobTemplateService;

    @Override
    public void updateEntity(ManageEntityVo manageEntityVo, String json) {
        SpFobSubleaseRedeliveryUpdate object = JsonUtils.jsonToObject(json,SpFobSubleaseRedeliveryUpdate.class);
        SpFobSubleaseRedeliveryUpdate his = mapperapper.selectOne(new LambdaQueryWrapper<SpFobSubleaseRedeliveryUpdate>().eq(SpFobSubleaseRedeliveryUpdate::getProceeInstanceId,manageEntityVo.getProcessInstanceId()).ne(SpFobSubleaseRedeliveryUpdate::getStatus,-2));
        if(his==null){ // 新增
            object.setSchemeDetailId(manageEntityVo.getSchemeDetailId());
            object.setTaskId(manageEntityVo.getTaskId());
            object.setProceeInstanceId(manageEntityVo.getProcessInstanceId());
            mapperapper.insert(object);
        }else { // 修改
            object.setId(his.getId());
            BeanUtils.copyProperties(object,his);
            mapperapper.updateById(his);
            // 处理多余文件记录
            List<String> ids = new ArrayList<>();
            if(his.getNoticeFile()!=null && his.getNoticeFile().length()>0){
                String[] fieldIds = his.getNoticeFile().split(",");
                ids =  Arrays.stream(fieldIds).collect(Collectors.toList());
            }
            if(his.getFiels()!=null && his.getFiels().length()>0){
                String[] fieldIds = his.getFiels().split(",");
                ids.addAll(Arrays.stream(fieldIds).collect(Collectors.toList()));
            }
            spFobFileRecordService.deleteOtherFileRecor(his.getId(),ids);
        }

    }

    @Override
    public SpFobBaseBean getBaseBeanByInstanceId(String id) {
        return mapperapper.selectOne(new LambdaQueryWrapper<SpFobSubleaseRedeliveryUpdate>().eq(SpFobSubleaseRedeliveryUpdate::getProceeInstanceId,id).ne(SpFobSubleaseRedeliveryUpdate::getStatus,-2));
    }
    @Override
    public void  saveEntity(SpFobBaseBean bean){
        SpFobSubleaseRedeliveryUpdate spFob = new SpFobSubleaseRedeliveryUpdate();
        BeanUtils.copyProperties(bean,spFob);
        this.save(spFob);
    }

    @Override
    public void updateEntityById(SpFobBaseBean bean) {
        SpFobSubleaseRedeliveryUpdate spFob = new SpFobSubleaseRedeliveryUpdate();
        BeanUtils.copyProperties(bean,spFob);
        this.updateById(spFob);
    }


    @Override
    public Object getFormData(String id,Long serviceId) {
        SpFobSubleaseRedeliveryUpdate spFobZzgcjr = null;
        if(serviceId!=null){
            spFobZzgcjr = mapperapper.selectById(serviceId);
        }else{
            spFobZzgcjr = mapperapper.selectOne(new LambdaQueryWrapper<SpFobSubleaseRedeliveryUpdate>().eq(SpFobSubleaseRedeliveryUpdate::getProceeInstanceId,id).ne(SpFobSubleaseRedeliveryUpdate::getStatus,-2));
        }
        if(spFobZzgcjr==null){
            return null;
        }
        Map<String,Object> map = JsonUtils.jsonToMap(JsonUtils.objectToJson(spFobZzgcjr), Object.class);
        if(spFobZzgcjr.getNoticeFile()!=null){
            String[] ids = spFobZzgcjr.getNoticeFile().split(",");
            List<Long> resId = new ArrayList<>();
            for(String tempId:ids){
                if (StringUtils.isNotBlank(tempId)) {
                    resId.add(Long.parseLong(tempId));
                }
            }
            List<SpFobFileRecord> spFobFileRecordList =  spFobFileRecordService.listByIds(resId);
            map.put("noticeFile",spFobFileRecordList);
        }
        if(StringUtils.isNotBlank(spFobZzgcjr.getFiels())){
            String[] ids = spFobZzgcjr.getFiels().split(",");
            List<Long> resId = new ArrayList<>();
            for(String tempId:ids){
                if (StringUtils.isNotBlank(tempId)) {
                    resId.add(Long.parseLong(tempId));
                }
            }
            List<SpFobFileRecord> spFobFileRecordList =  spFobFileRecordService.listByIds(resId);
            map.put("fiels",spFobFileRecordList);
        }
        return map;
    }
    @Override
    public List<Long> findServieData(String processInstanceId){
        List<SpFobSubleaseRedeliveryUpdate> spFobBaseBeans =  mapperapper.selectList(new LambdaQueryWrapper<SpFobSubleaseRedeliveryUpdate>().eq(SpFobSubleaseRedeliveryUpdate::getProceeInstanceId,processInstanceId).orderByDesc(SpFobSubleaseRedeliveryUpdate::getCreateDate));
        List<Long> list = spFobBaseBeans.stream().map(SpFobBaseBean::getId).collect(Collectors.toList());
        return list;
    }

    @Override
    public SpFobSubleaseRedeliveryUpdate findData(String processInstanceId) {
        return   mapperapper.selectOne(new LambdaQueryWrapper<SpFobSubleaseRedeliveryUpdate>().eq(SpFobSubleaseRedeliveryUpdate::getProceeInstanceId,processInstanceId).ne(SpFobSubleaseRedeliveryUpdate::getStatus,-2));
    }

    @Override
    public void createPayOrder(Long id) {
        SpFobSubleaseRedeliveryUpdate his = this.getById(id);
        if(his!=null && his.getOrderId()==null){
            Long orderId = createPayOrder(his.getProceeInstanceId(),his);
            his.setOrderId(orderId);
            this.updateById(his);
        }
    }
    @Autowired
    TzFuelDeliveryService tzFuelDeliveryService;
    @Autowired
    SpFobSubleaseService spFobSubleaseService;
    @Autowired
    ConShippingService conShippingService;

    public Long createPayOrder(String processInstanceId, SpFobSubleaseRedeliveryUpdate his){
        SpFobSublease execute = spFobSubleaseService.getExceuteByInstanceId(processInstanceId);
        if(execute==null){
            throw  new BusException("流程业务不存在，请重新核对", BaseResultCode.GENERAL_ERROR);
        }
        ConShipping conShipping = conShippingService.getById(execute.getConId());
        if(conShipping==null){
            throw  new BusException("关联合同信息错误，请重新核对", BaseResultCode.GENERAL_ERROR);
        }
        TzFuelDelivery record = new TzFuelDelivery();
        record.setImo(execute.getImo());
        record.setShipName(execute.getShipName());
        record.setLessee(conShipping.getTenant());
        record.setShipOwner(conShipping.getShipowner());
        record.setDeliveryType(2);
        record.setContractType(conShipping.getContractType()+"");
        if(StringUtils.isNotEmpty(his.getLsmgo())){
            record.setLsmgo(new BigDecimal(his.getLsmgo()));
        }
        if(StringUtils.isNotEmpty(his.getLsmgoPrice())){
            record.setLsmgoPrice(new BigDecimal(his.getLsmgoPrice()));
        }
        if(StringUtils.isNotEmpty(his.getVisfo())){
            record.setVlsfo(new BigDecimal(his.getVisfo()));
        }
        if(StringUtils.isNotEmpty(his.getVisfoPrice())){
            record.setVlsfoPrice(new BigDecimal(his.getVisfoPrice()));
        }

        if(StringUtils.isNotEmpty(his.getLsmgo2())){
            record.setLsmgoTwo(new BigDecimal(his.getLsmgo2()));
        }
        if(StringUtils.isNotEmpty(his.getLsmgoPrice2())){
            record.setLsmgoTwoPrice(new BigDecimal(his.getLsmgoPrice2()));
        }
        if(StringUtils.isNotEmpty(his.getVisfo2())){
            record.setVlsfoTwo(new BigDecimal(his.getVisfo2()));
        }
        if(StringUtils.isNotEmpty(his.getVisfoPrice2())){
            record.setVlsfoTwoPrice(new BigDecimal(his.getVisfoPrice2()));
        }

        if(StringUtils.isNotEmpty(his.getLsmgo3())){
            record.setLsmgoThree(new BigDecimal(his.getLsmgo3()));
        }
        if(StringUtils.isNotEmpty(his.getLsmgoPrice3())){
            record.setLsmgoThreePrice(new BigDecimal(his.getLsmgoPrice3()));
        }
        if(StringUtils.isNotEmpty(his.getVisfo3())){
            record.setVlsfoThree(new BigDecimal(his.getVisfo3()));
        }
        if(StringUtils.isNotEmpty(his.getVisfoPrice3())){
            record.setVlsfoThreePrice(new BigDecimal(his.getVisfoPrice3()));
        }

        record.setDeliveryTime(his.getRedeliveryDate());
        record.setFileInfo(his.getNoticeFile());
        record.setProcessInstanceId(processInstanceId);
        record.setRemark(his.getRemark());
        Long orderId = tzFuelDeliveryService.dataTransfer(record);
        return orderId;
    }

}
